﻿package cn.rayyee.net 
{
	import flash.display.Sprite;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.external.ExternalInterface;
	/**
	 * ...GetUrl类
	 * @author rayYee
	 * @example GetUrl("http://www.riavs.cn/blog","_blank")
	 * 
	 * 	url:String - 指定要导航到哪个 URL。 
	
		window:String (default = null) — 浏览器窗口或 HTML 帧，其中显示 request 参数指示的文档。 可以输入某个特定窗口的名称，或使用以下值之一： 
		"_self" 指定当前窗口中的当前帧。
		"_blank" 指定一个新窗口。
		"_parent" 指定当前帧的父级。
		"_top" 指定当前窗口中的顶级帧。 
		
		features:String - 参数

	 */
	public class GetUrl {
	public function GetUrl(url:String, window:String="_blank", features:String="") {
		var WINDOW_OPEN_FUNCTION:String = "window.open";
		var myURL:URLRequest = new URLRequest(url);
		var browserName:String = getBrowserName();

		if (getBrowserName() == "Firefox") {//Firefox
			ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
		} else if (browserName == "IE") {//If IE, 
			ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
			//ExternalInterface.call("function setWMWindow() {window.open(&apos;" + url + "&apos;);}");
		} else if (browserName == "Safari") {//If Safari     
			navigateToURL(myURL, window);
		} else if (browserName == "Opera") {//If Opera 
			navigateToURL(myURL, window);
		} else {
			navigateToURL(myURL, window);
		}
			/*Alternate methodology...
			      var popSuccess:Boolean = ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features); 
			     if(popSuccess == false){
			    navigateToURL(myURL, window);
			    }*/
		}
	private function getBrowserName():String {
		var browser:String;
			
		//Uses external interface to reach out to browser and grab browser useragent info.
		var browserAgent:String = ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");

		//  Debug.text += "Browser Info: [" + browserAgent + "]";

		//Determines brand of browser using a find index. If not found indexOf returns (-1).
		if (browserAgent != null && browserAgent.indexOf("Firefox") >= 0) {
			browser = "Firefox";
		} else if (browserAgent != null && browserAgent.indexOf("Safari") >= 0) {
			browser = "Safari";
		} else if (browserAgent != null && browserAgent.indexOf("MSIE") >= 0) {
			browser = "IE";
		} else if (browserAgent != null && browserAgent.indexOf("Opera") >= 0) {
			browser = "Opera";
		} else {
			browser = "Undefined";
		}
		return browser;
	}
	}
}